import { constantRoutes } from '@/router'
import { getRouters } from '@/api/menu'
import Layout from '@/layout/index'

const permission = {
  state: {
    routes: [],
    addRoutes: []
  },
  mutations: {
    SET_ROUTES: (state, routes) => {
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)
    }
  },
  actions: {
    // 生成路由
    GenerateRoutes({ commit }) {
      return new Promise(resolve => {
        // 向后端请求路由数据
        let data = [
        //   {
        //   "name": "Monitor",
        //   "path": "/monitor",
        //   "hidden": false,
        //   "redirect": "noRedirect",
        //   "component": "Layout",
        //   "alwaysShow": true,
        //   "meta": {
        //     "title": "系统监控",
        //     "icon": "monitor"
        //   },
        //   "children": [{
        //     "name": "Online",
        //     "path": "online",
        //     "hidden": false,
        //     component: () => import('@/views/monitor/online/index'),
        //     // "component": "monitor/online/index",
        //     "meta": {
        //       "title": "在线用户",
        //       "icon": "online"
        //     }
        //   }, {
        //     "name": "Job",
        //     "path": "job",
        //     "hidden": false,
        //     component: () => import('@/views/monitor/job/index'),
        //     // "component": "monitor/job/index",
        //     "meta": {
        //       "title": "定时任务",
        //       "icon": "job"
        //     }
        //   }, {
        //     "name": "Druid",
        //     "path": "druid",
        //     "hidden": false,
        //     component: () => import('@/views/monitor/druid/index'),
        //     // "component": "monitor/druid/index",
        //     "meta": {
        //       "title": "数据监控",
        //       "icon": "druid"
        //     }
        //   }, {
        //     "name": "Server",
        //     "path": "server",
        //     "hidden": false,
        //     component: () => import('@/views/monitor/server/index'),
        //     // "component": "monitor/server/index",
        //     "meta": {
        //       "title": "服务监控",
        //       "icon": "server"
        //     }
        //   }]
        // }, {
        //   "name": "Tool",
        //   "path": "/tool",
        //   "hidden": false,
        //   "redirect": "noRedirect",
        //   "component": "Layout",
        //   "alwaysShow": true,
        //   "meta": {
        //     "title": "系统工具",
        //     "icon": "tool"
        //   },
        //   "children": [{
        //     "name": "Build",
        //     "path": "build",
        //     "hidden": false,
        //     "component": "tool/build/index",
        //     "meta": {
        //       "title": "表单构建",
        //       "icon": "build"
        //     }
        //   }, {
        //     "name": "Gen",
        //     "path": "gen",
        //     "hidden": false,
        //     "component": "tool/gen/index",
        //     "meta": {
        //       "title": "代码生成",
        //       "icon": "code"
        //     }
        //   }]
        // }
      ]
        const accessedRoutes = filterAsyncRouter(data)
        accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
        commit('SET_ROUTES', accessedRoutes)
        resolve(accessedRoutes)
      })
    }
  }
}

// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
  return asyncRouterMap.filter(route => {
    if (route.component) {
      // Layout组件特殊处理
      if (route.component === 'Layout') {
        route.component = Layout
      } else {
        route.component = loadView(route.component)
      }
    }
    if (route.children != null && route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children)
    }
    return true
  })
}

export const loadView = (view) => { // 路由懒加载
  return (resolve) =>  require([`@/views/${view}`], resolve)
}

export default permission
